package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbIntegralLoss;
import com.gtw.modules.sys.service.SysIntegralLossService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/** 积分日耗Controller
 *
 * @author wuye
 * @date 2018/1/31
 */
@RestController
@RequestMapping("/sys/integral")
public class SysIntegralLossController extends AbstractController{

    @Autowired
    private SysIntegralLossService integralLossService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:integral:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbIntegralLoss> integralLossList = integralLossService.queryList(query);
        int total = integralLossService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(integralLossList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:integral:info")
    public R info(@PathVariable("id") Integer id){
        TbIntegralLoss integralLoss = integralLossService.queryObject(id);

        return R.ok().put("integralLoss", integralLoss);
    }

    /**
     * 保存
     */
    @SysLog("添加积分日耗")
    @RequestMapping("/save")
    @RequiresPermissions("sys:integral:save")
    public R save(@RequestBody TbIntegralLoss integralLoss){
        integralLoss.setCreatetime(new Date());
        integralLoss.setCreateuser(getUser().getUsername());
        integralLoss.setUpdatetime(new Date());
        integralLoss.setUpdateuser(getUser().getUsername());
        integralLoss.setEnabled(0);
        integralLossService.save(integralLoss);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改积分日耗")
    @RequestMapping("/update")
    @RequiresPermissions("sys:integral:update")
    public R update(@RequestBody TbIntegralLoss integralLoss){
        integralLoss.setUpdatetime(new Date());
        integralLoss.setUpdateuser(getUser().getUsername());
        integralLossService.update(integralLoss);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除积分日耗")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:integral:delete")
    public R delete(@RequestBody Integer[] ids){
        integralLossService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 启用
     * @return
     */
    @SysLog("启用积分日耗")
    @RequestMapping("/enabled/{id}")
    @RequiresPermissions("sys:integral:enabled")
    public R enabled(@PathVariable("id") int id){
        TbIntegralLoss tbIntegralLoss = integralLossService.queryObject(id);
        tbIntegralLoss.setEnabled(1);
        integralLossService.update(tbIntegralLoss);
        return R.ok();
    }

    /**
     * 禁用
     * @return
     */
    @SysLog("禁用积分日耗")
    @RequestMapping("/enabled2/{id}")
    @RequiresPermissions("sys:integral:enabled2")
    public R enabled2(@PathVariable("id") int id){
        TbIntegralLoss tbIntegralLoss = integralLossService.queryObject(id);
        tbIntegralLoss.setEnabled(1);
        integralLossService.update(tbIntegralLoss);
        return R.ok();
    }
}
